<template>
	<el-header style="height: auto;">
		<sc-select-filter :data="filterData" :label-width="80" @on-change="filterChange"></sc-select-filter>
	</el-header>
	<el-header>
		<div class="left-panel">
		</div>
		<div class="right-panel">
			<el-form-item label="发起人" style="padding-top: 20px;">
				<select-user-dialog v-model:accountId="search.createUser" :mode="false"/>
			</el-form-item>
			<el-form-item label="流水号" style="padding-top: 20px;">
				<el-input v-model="search.bpmRunNo" placeholder="流水号" clearable></el-input>
			</el-form-item>
			<div class="right-panel-search" style="width: auto;">
				<el-input v-model="search.keyword" placeholder="流程标题" clearable></el-input>
				<el-button type="primary" icon="el-icon-search" @click="upsearch"></el-button>
			</div>
		</div>
	</el-header>
	<el-main class="nopadding">
		<scTable ref="table" :params="{model:'0'}" :apiObj="list.apiObj" row-key="runId" stripe>
			<el-table-column label="流水号" prop="id" width="80"></el-table-column>
			<el-table-column label="流程标题" prop="flowTitle" width="400" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="步骤名称" prop="stepName" width="200" align="center" :show-overflow-tooltip="true"></el-table-column>
			<el-table-column label="处理人" prop="opUserName" width="100" align="center"></el-table-column>
			<el-table-column label="创建时间" prop="createTime" width="150" align="center"></el-table-column>
			<el-table-column label="接收时间" prop="recTime" width="150" align="center"></el-table-column>
			<el-table-column label="结束时间" prop="endTime" width="150" align="center"></el-table-column>
			<el-table-column label="步骤类型" prop="opFlag" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.opFlag==0" type="success">主办</el-tag>
					<el-tag v-else-if="scope.row.opFlag==1" type="warning">经办</el-tag>
					<el-tag v-else type="info">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="状态" prop="sealedStatus" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="scope.row.status==0" type="success">进行中</el-tag>
					<el-tag v-else-if="scope.row.status==1" type="warning">已结束</el-tag>
					<el-tag v-else type="info">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="办理时限" prop="passTime" width="100">
				<template #default="scope">
					<el-tag v-if="scope.row.passTime==0" type="success" effect="dark">不限</el-tag>
					<el-tag v-else type="warning" effect="dark">{{scope.row.passTime}}小时</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="是否超时" prop="passTime" width="100" align="center">
				<template #default="scope">
					<el-tag v-if="testPassTime(scope.row)=='1'" type="success" effect="dark">正常</el-tag>
					<el-tag v-else-if="testPassTime(scope.row)=='0'" type="danger" effect="dark">超时</el-tag>
					<el-tag v-else type="info" effect="dark">未知</el-tag>
				</template>
			</el-table-column>
			<el-table-column label="操作" fixed="right" align="right" width="100">
				<template #default="scope">
					<el-button-group>
						<el-button text type="primary" size="small" @click="readBpmDetails(scope.row,scope.$index)">查看</el-button>
					</el-button-group>
				</template>
			</el-table-column>
		</scTable>
	</el-main>
</template>
<script>
import scSelectFilter from '@/components/scSelectFilter'
import selectUserDialog from "@/views/module/selectUserDialog";

export default {
	name: 'Bpm Monitor',
	components: {
		scSelectFilter,
		selectUserDialog
	},
	data() {
		return {
			filterData: [
				{
					title: "日期范围",
					key: "date",
					multiple: false,
					options: [
						{
							label: "全部",
							value: ""
						},
						{
							label: "今日",
							value: "1"
						},
						{
							label: "昨日",
							value: "2"
						},
						{
							label: "一周内",
							value: "3"
						},
						{
							label: "一月内",
							value: "4"
						},
						{
							label: "一月前",
							value: "5"
						}
					]
				},
				{
					title: "流程分类",
					key: "flowId",
					multiple: true,
					options: [{
						label: "全部",
						value: ""
					}]
				}
			],
			list: {
				apiObj: this.$API.bpm.bpmList.getBpmFlowForMonitor
			},
			search: {
				model:'0',
				date:'',
				flowId:'',
				keyword: "",
				bpmRunNo: '',
				createUser: '',
			}
		}
	},
	mounted() {
		this.getBpmFlowListForSelect();
	},
	methods: {
		//搜索
		upsearch() {
			this.$refs.table.upData(this.search)
		},
		filterChange(data) {
			this.search.date = data.date;
			this.search.flowId = data.flowId;
			this.$refs.table.upData(this.search)
		},
		async getBpmFlowListForSelect() {
			var res = await this.$API.bpm.bpmFlow.getBpmFlowListForSelect.get({model:'0'});
			if (res.code == 200) {
				this.filterData[1].options = this.filterData[1].options.concat(res.data)
			} else {
				this.$alert(res.message, "提示", {type: 'error'})
			}
		},
		testPassTime(row) {
			if(row.status=='0')
			{
				let createTime = row.createTime;
				let passTime = row.passTime;
				if (passTime != 0) {
					let timestamp1 = Date.parse(new Date(createTime)) + passTime * 60*60*1000;
					let timestamp = Date.parse(new Date());
					if (timestamp1 > timestamp) {
						return "1"
					} else {
						return "0";
					}
				} else {
					return "1";
				}
			}else{
				let recTime = row.recTime;
				let createTime = row.createTime;
				let passTime = row.passTime;
				if (passTime != 0) {
					if (Date.parse(new Date(recTime)) - Date.parse(new Date(createTime)) > passTime * 60 * 60 * 1000) {
						return "0";
					} else {
						return "1";
					}
				}else{
					return "1";
				}
			}
		},
		readBpmDetails(row,index)
		{
			this.$nextTick(() => {
				this.$router.replace({
					path: '/bpm/handle/read',
					query: {
						runId: row.runId
					}
				})
			})
		},
	}
}
</script>
